package org.file5.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.file5.dao.DaoFascade;
import org.file5.domain.Category;
import org.file5.service.CategoryService;
import org.file5.util.DataBean;
import org.file5.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.googlecode.objectify.Key;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	DaoFascade df;

	@Override
	public DataBean<Category> getCategories(int startPosition, int maxResult,
			Object... params) {

		return df.getCategoryDao().getCategories(startPosition, maxResult,
				params);
	}

	@Override
	public List<Category> getPrimaryCategoryTrail(Key<Category> categoryKey) {
		return df.getCategoryDao().getPrimaryCategoryTrail(categoryKey);
	}

	@Override
	public Category find(String categoryUid) {
		Category result = null;
		if (categoryUid != null && !"".equals(categoryUid)) {
			Key<Category> gkey = new Key<Category>(Category.class, categoryUid);
			result = df.getBaseDao().find(Category.class, gkey);
			if (result != null) {
				List<Category> primaryCategoryTrail = df.getCategoryDao()
						.getPrimaryCategoryTrail(gkey);
				result.setPrimaryCategoryTrail(primaryCategoryTrail);

				DataBean<Category> dataBean = df.getCategoryDao()
						.getCategories(0, DataBean.MAX_CHILD_CATEGORIES,
								categoryUid);
				if (dataBean != null && !dataBean.isEmpty()) {
					result.setChildCategories(dataBean.getData());
				}
			}
		}
		return result;
	}

	@Override
	public Category findTopGeneralProductCategory() {
		Category g = this.find(TOP_GENERAL_PRODUCT_CATEGORY_UID);
		if (g == null) {
			g = new Category();
			g.setUid(TOP_GENERAL_PRODUCT_CATEGORY_UID);
			g.setName("全部产品");
			g = df.getBaseDao().addUpdate(g);
		} else {
			g = this.find(TOP_GENERAL_PRODUCT_CATEGORY_UID);
		}
		return g;
	}

	@Override
	public Category findTopSiteProductCategory() {
		Category g = this.find(TOP_SITE_PRODUCT_CATEGORY_UID);
		if (g == null) {
			g = new Category();
			g.setUid(TOP_SITE_PRODUCT_CATEGORY_UID);
			g.setName("站内产品");
			g = df.getBaseDao().addUpdate(g);
		} else {
			g = this.find(TOP_SITE_PRODUCT_CATEGORY_UID);
		}
		return g;
	}

	@Override
	public Category findTopSiteArticleCategory() {
		Category g = this.find(TOP_SITE_ARTICLE_CATEGORY_UID);
		if (g == null) {
			g = new Category();
			g.setUid(TOP_SITE_ARTICLE_CATEGORY_UID);
			g.setName("全部博文");
			g = df.getBaseDao().addUpdate(g);
		} else {
			g = this.find(TOP_SITE_ARTICLE_CATEGORY_UID);
		}
		return g;
	}

	@Override
	public Category setPrimaryParent(Category category, Category parent) {
		if (category != null && parent != null
				&& !category.getUid().equals(parent.getUid())) {
			category.setPrimaryParentKey(new Key<Category>(Category.class,
					parent.getUid()));
			df.getBaseDao().addUpdate(category);
		}
		return category;
	}

	@Override
	public DataBean<Category> getChildCategories(int startPosition, int maxResult,String categoryUid) {
		return df.getCategoryDao().getChildCategories(startPosition,maxResult,categoryUid);
	}

	@Override
	public Category newChildCategory(String parentCategoryUid) {

		Category g = new Category();

		g.setUid(UUIDUtil.getRandomUUID());

		// g.setType(type);
		g.setName("新建文件夹");
		Category pg = df.getBaseDao().find(Category.class, parentCategoryUid);
		// sf.getBaseService().find(Category.class,
		// parentCategoryUid);
		if (pg != null) {
			g.setPrimaryParentKey(new Key<Category>(Category.class,
					parentCategoryUid));
		} else {
			pg = this.findTopSiteArticleCategory();
			g.setPrimaryParentKey(new Key<Category>(Category.class, pg.getUid()));
		}
		df.getBaseDao().addUpdate(g);
		return g;

	}

}
